from rest_framework.views import Response,APIView
from goods.models import *
from datetime import datetime


class AddCombo(APIView):
    def post(self,request):
        s_name = request.data.get('s_name')
        s_img = request.data.get('s_img')
        classify = request.data.get('classify')
        price = request.data.get('price')
        status = request.data.get('status')
        if not all([s_name,s_img,classify,price,status]):
            return Response({'msg':'套餐信息不能为空', 'code':203})
        Combo.objects.create(s_name=s_name,
                             s_img=s_img,
                             classify=classify,
                             price=price,
                             status=status)
        return Response({'msg':'添加套餐信息成功', 'code':200})


class DelCombo(APIView):
    def delete(self,request):
        id = request.data.get('id')
        Combo.objects.filter(id=id).delete()
        return Response({'msg':'删除套餐信息成功', 'code':200})


class GetCombo(APIView):
    def get(self,request):
        c_data = Combo.objects.all()
        list8 = []
        for i in c_data:
            data8 = {
                'id':i.id,
                's_name':i.s_name,
                's_img':i.s_img,
                'classify':i.classify,
                'price':i.price,
                'status':i.status,
                'end_time':i.end_time
            }
            list8.append(data8)
        return Response({'msg':'获取套餐信息成功', 'code':200, 'data':list8})


class UpdateCombo(APIView):
    def put(self,request):
        id = request.data.get('id')
        s_name = request.data.get('s_name')
        s_img = request.data.get('s_img')
        classify = request.data.get('classify')
        price = request.data.get('price')
        status = request.data.get('status')
        Combo.objects.filter(id=id).update(s_name=s_name,
                                             s_img=s_img,
                                             classify=classify,
                                             price=price,
                                             status=status)
        return Response({'msg':'修改套餐信息成功', 'code':200})